/**
 * 
 */
package com.ieasy.module.oa.project.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ieasy.basic.dao.IBaseDao;
import com.ieasy.basic.exception.ServiceException;
import com.ieasy.basic.model.DataGrid;
import com.ieasy.basic.model.Msg;
import com.ieasy.basic.model.Pager;
import com.ieasy.basic.model.SystemContext;
import com.ieasy.basic.util.BeanUtils;
import com.ieasy.module.common.service.BaseService;
import com.ieasy.module.oa.project.entity.CompanyProjectQuotientEntity;
import com.ieasy.module.oa.project.web.form.CompanyProjectQuotientForm;

/**
 * @author 杨浩泉
 *
 */
@Service @Transactional
public class CompanyProjectQuotientService extends BaseService implements ICompanyProjectQuotientService {
	
	@Inject
	private IBaseDao<CompanyProjectQuotientEntity> basedaoCompanyQuotient ;
	
	@Override
	public Msg add(CompanyProjectQuotientForm form) {
		CompanyProjectQuotientEntity entity = new CompanyProjectQuotientEntity() ;
		BeanUtils.copyNotNullProperties(form, entity) ;
		entity.setCreated(new Date()) ;
		BeanUtils.copyNotNullProperties(form, entity) ;
		this.basedaoCompanyQuotient.add(entity) ;
		return new Msg(true, "添加成功！");
	}

	@Override
	public Msg delete(CompanyProjectQuotientForm form) {
		this.basedaoCompanyQuotient.delete(CompanyProjectQuotientEntity.class, form.getId()) ;
		return new Msg(true, "删除成功！");
	}
	
	@Override
	public Msg update(CompanyProjectQuotientForm form) {
		try {
			CompanyProjectQuotientEntity entity = this.basedaoCompanyQuotient.load(CompanyProjectQuotientEntity.class, form.getId()) ;
			BeanUtils.copyNotNullProperties(form, entity) ;
			entity.setModifyDate(new Date()) ;
			entity.setModifyName(this.getCurrentUser().getEmp_name()) ;
			
			this.basedaoCompanyQuotient.update(entity) ;
			return new Msg(true, "修改成功！");
		} catch (Exception e) {
			return new Msg(false, "修改信息失败！") ;
		}
	}
	
	@Override
	public CompanyProjectQuotientForm get(String id) {
		CompanyProjectQuotientEntity entity = this.basedaoCompanyQuotient.load(CompanyProjectQuotientEntity.class, id) ;
		CompanyProjectQuotientForm form = new CompanyProjectQuotientForm() ;
		BeanUtils.copyNotNullProperties(entity, form) ;
		return form ;
	}

	@Override
	public DataGrid datagrid(CompanyProjectQuotientForm form) {
		
		SystemContext.setSort("t.year") ;SystemContext.setOrder("desc") ;
		try {
			Pager<CompanyProjectQuotientEntity> pager = this.find(form) ;
			List<CompanyProjectQuotientForm> forms = new ArrayList<CompanyProjectQuotientForm>() ;
			for (CompanyProjectQuotientEntity entity : pager.getDataRows()) {
				CompanyProjectQuotientForm gf = new CompanyProjectQuotientForm() ;
				BeanUtils.copyNotNullProperties(entity, gf) ;
				forms.add(gf) ;
			}
			
			DataGrid dg = new DataGrid() ;
			dg.setTotal(pager.getTotal());
			dg.setRows(forms);
			return dg;
		} catch (Exception e) {
			throw new ServiceException("加载信息异常", e) ;
		}
	}


	private Pager<CompanyProjectQuotientEntity> find(CompanyProjectQuotientForm form) {
		Map<String, Object> alias = new HashMap<String, Object>();
		String hql = "select t from CompanyProjectQuotientEntity t where 1=1 " ;
		hql = addWhere(hql, form, alias) ;
		return this.basedaoCompanyQuotient.find(hql, alias) ;
	}

	private String addWhere(String hql, CompanyProjectQuotientForm form, Map<String, Object> params) {
		if (form.getYear() != null && !"".equals(form.getYear())) {
			hql += " and t.year=:year";
			params.put("year", form.getYear());
		}
		return hql;
	}

	@Override
	public CompanyProjectQuotientForm getByYeay(Integer year) {
		return (CompanyProjectQuotientForm) this.basedaoCompanyQuotient.queryObjectSQL("select t.* from ieasy_oa_company_quotient t where t.year=?", new Object[]{year}, CompanyProjectQuotientForm.class, false);
	}

}
